﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace FindMyChamp.Helpers
{
    public static class Animate
    {
        public static void LoadContent(PhoneApplicationPage page, UIElement item, Action callback)
        {
            if (item.Visibility != Visibility.Collapsed)
                return;
            // throw new Exception("item.Visibility != Collapsed");

            var _TopFrom = page.RenderSize.Height;
            var _TopTo = 0;
            var _OpacityFrom = 0;
            var _OpacityTo = 1;

            ShowHide(item, () => { if (callback != null) callback(); },
                _TopFrom, _TopTo, _OpacityFrom, _OpacityTo);
        }

        public static void UnloadContent(PhoneApplicationPage page, UIElement item, Action callback)
        {
            var _TopFrom = 0;
            var _TopTo = page.RenderSize.Height;
            var _OpacityFrom = 1;
            var _OpacityTo = 0;

            ShowHide(item, () => { if (callback != null) callback(); },
                _TopFrom, _TopTo, _OpacityFrom, _OpacityTo);
        }

        private static void ShowHide(UIElement item, Action callback, double topFrom, double topTo, double opacityFrom, double opacityTo)
        {
            // setup
            var _Translate = new TranslateTransform
            {
                X = 0,
                Y = topFrom
            };
            item.RenderTransform = _Translate;
            item.Visibility = Visibility.Visible;
            item.Opacity = opacityFrom;

            // animate
            var _Storyboard = new Storyboard();
            var _Duration = TimeSpan.FromSeconds(.5);

            // opacity
            var _OpacityAnimate = new DoubleAnimation
            {
                To = opacityTo,
                Duration = _Duration,
            };
            _Storyboard.Children.Add(_OpacityAnimate);
            Storyboard.SetTarget(_OpacityAnimate, item);
            Storyboard.SetTargetProperty(_OpacityAnimate,
                new PropertyPath(UIElement.OpacityProperty));

            // translate (location)
            var _TranslateAnimate = new DoubleAnimation
            {
                To = topTo,
                Duration = _Duration,
                EasingFunction = new SineEase
                {
                    EasingMode = EasingMode.EaseOut
                }
            };
            _Storyboard.Children.Add(_TranslateAnimate);
            Storyboard.SetTarget(_TranslateAnimate, _Translate);
            Storyboard.SetTargetProperty(_TranslateAnimate,
                new PropertyPath(TranslateTransform.YProperty));

            // finalize
            _TranslateAnimate.Completed += (s, arg) =>
            {
                if (callback != null)
                    callback();
            };
            _Storyboard.Begin();
        }

        public static void Delete(PhoneApplicationPage page, UIElement item, Action callback)
        {
            // setup
            var _Scale = new ScaleTransform
            {
                ScaleX = 1,
                ScaleY = 1,
                CenterX = item.RenderSize.Width / 2,
                CenterY = item.RenderSize.Height / 2,
            };
            var _Translate = new TranslateTransform { X = 0, Y = 0 };
            var _Group = new TransformGroup();
            _Group.Children.Add(_Scale);
            _Group.Children.Add(_Translate);
            item.RenderTransform = _Group;

            // animate
            var _Storyboard = new Storyboard { };

            // scale X
            var _ScaleAnimateX = new DoubleAnimation
            {
                To = .75,
                Duration = TimeSpan.FromSeconds(.25)
            };
            _Storyboard.Children.Add(_ScaleAnimateX);
            Storyboard.SetTarget(_ScaleAnimateX, _Scale);
            Storyboard.SetTargetProperty(_ScaleAnimateX,
                new PropertyPath(ScaleTransform.ScaleXProperty));

            // scale Y
            var _ScaleAnimateY = new DoubleAnimation
            {
                To = .75,
                Duration = TimeSpan.FromSeconds(.25)
            };
            _Storyboard.Children.Add(_ScaleAnimateY);
            Storyboard.SetTarget(_ScaleAnimateY, _Scale);
            Storyboard.SetTargetProperty(_ScaleAnimateY,
                new PropertyPath(ScaleTransform.ScaleYProperty));

            // translate (location)
            var _TranslateAnimate = new DoubleAnimation
            {
                To = page.RenderSize.Height,
                BeginTime = TimeSpan.FromSeconds(.45),
                Duration = TimeSpan.FromSeconds(.25)
            };
            _Storyboard.Children.Add(_TranslateAnimate);
            Storyboard.SetTarget(_TranslateAnimate, _Translate);
            Storyboard.SetTargetProperty(_TranslateAnimate,
                new PropertyPath(TranslateTransform.YProperty));

            // finalize
            _TranslateAnimate.Completed += (s, arg) =>
            {
                if (callback != null)
                    callback();
            };
            _Storyboard.Begin();
        }

        public static void PressIn(UIElement item, Action callback)
        {
            Resize(item, callback, 1, .75);
        }

        private static void Resize(UIElement item, Action callback, double start, double target)
        {
            if (item == null)
                return;

            // when user pushes a resolution, animate it smaller
            var _Transform = new ScaleTransform
            {
                CenterX = item.RenderSize.Width * .5,
                CenterY = item.RenderSize.Height * .5,
                ScaleX = start,
                ScaleY = start
            };
            item.RenderTransform = _Transform;
            var _Storyboard = new Storyboard { AutoReverse = false };

            // Y
            var _AnimationY = new DoubleAnimation
            {
                Duration = TimeSpan.FromSeconds(.1),
                To = target
            };
            _Storyboard.Children.Add(_AnimationY);
            Storyboard.SetTarget(_AnimationY, _Transform);
            Storyboard.SetTargetProperty(_AnimationY, new PropertyPath(ScaleTransform.ScaleYProperty));

            // X
            var _AnimationX = new DoubleAnimation
            {
                Duration = TimeSpan.FromSeconds(.1),
                To = target
            };
            _Storyboard.Children.Add(_AnimationX);
            Storyboard.SetTarget(_AnimationX, _Transform);
            Storyboard.SetTargetProperty(_AnimationX, new PropertyPath(ScaleTransform.ScaleXProperty));
            _Storyboard.Completed += (s, arg) =>
            {
                if (callback != null)
                    callback();
            };

            // Go!
            _Storyboard.Begin();
        }

        public static void Vanish(UIElement item, Action callback)
        {
            Resize(item, callback, 1, 0);
        }
    }
}
